import requests,pprint

def login(username,password):
    payload ={
        'username':username,
        'password':password
        }
    r=requests.post("http://localhost/api/mgr/loginReq",data=payload)
    retobj = r.json()
    pprint.pprint(retobj)
    return retobj,r.cookies
login('auto','sdfsdfsdf')

#列出课程
def list_course():
    payload={
        'action': 'list_course',
        'pagenum': 1,
        'pagesize': 20
    }

    # headers1={
    #     'username':'liaogan',
    #     'password':'777888'
    # }
    r=requests.get("http://localhost/api/mgr/sq_mgr/",params=payload)
    retobj=r.json()
    pprint.pprint(retobj)
    # print(retobj['retlist'])
    # isget=False
    # for course in retobj['retlist']:
    #     if course['name']=='英文':
    #         isget=True
    #         break
    # assert isget
    return  retobj

# list_course()


#添加课程
def add_course(name,desc,idx):
    payload={
        'action': 'add_course',
        'data':'''{
        "name":"%s",
        "desc":"%s",
        "display_idx":"%s"
        }'''%(name,desc,idx)
    }

    r=requests.post("http://localhost/api/mgr/sq_mgr/",data=payload)
    retobj=r.json()
    pprint.pprint(retobj)
    return retobj
# add_course('python2','python课程','3')


#删除课程
def delete_course(cid):
    payload={
        'action':'delete_course',
        'id'    :cid

    }

    r=requests.delete('http://localhost/api/mgr/sq_mgr/',data=payload)
    retobj=r.json()
    pprint.pprint(retobj)
    return retobj
# delete_course(1347)

#修改课程
def modify_course(cid,name,desc,idx):
    payload={
        'action':'modify_course',
        'id'    :cid,
        'newdata':'''{
        "name":"%s",
        "desc":"%s",
        "display_idx":"%s"
        } '''%(name,desc,idx)
    }

    r=requests.put('http://localhost/api/mgr/sq_mgr/',data=payload)
    retobj=r.json()
    pprint.pprint(retobj)
    return retobj

#添加json格式的请求
def addjsoncourse():
    payload={
  "action" : "add_course_json",
  "data"	 : {
    "name":"初中化学11",
    "desc":"初中化学课程111",
    "display_idx":"4"
  }
}

    r=requests.post('http://localhost/apijson/mgr/sq_mgr/',json=payload)
    retobj=r.json()
    pprint.pprint(retobj)
    return retobj
addjsoncourse()
list_course()

